package com.sz.lk.wingsystem.mutils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;
import android.util.Log;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

/**
 * Created by liuke on 2017/10/31.
 * shareference做的本地保存工具类
 */

public class SaveUtils {
    private String FILE_NAME = "userinfor";
    private static SaveUtils instance;

    public SaveUtils() {
    }

    public String getFILE_NAME() {
        return this.FILE_NAME;
    }

    public void setFILE_NAME(String fILE_NAME) {
        this.FILE_NAME = fILE_NAME;
    }

    public static SaveUtils getInstance() {
        if(instance == null) {
            instance = new SaveUtils();
        }

        return instance;
    }

    public void save(Context context, String key, Object object) {
        SharedPreferences sp = context.getSharedPreferences(this.FILE_NAME, 0);
        Editor editor = sp.edit();
        if(object instanceof String) {
            editor.putString(key, (String)object);
        } else if(object instanceof Integer) {
            editor.putInt(key, ((Integer)object).intValue());
        } else if(object instanceof Boolean) {
            editor.putBoolean(key, ((Boolean)object).booleanValue());
        } else if(object instanceof Float) {
            editor.putFloat(key, ((Float)object).floatValue());
        } else if(object instanceof Long) {
            editor.putLong(key, ((Long)object).longValue());
        } else {
            editor.putString(key, object.toString());
        }

        SharedPreferencesCompat.apply(editor);
    }

    public void saveObject(Context context, String key, Object obj) {
        try {
            Editor e = context.getSharedPreferences(this.FILE_NAME, 0).edit();
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream os = new ObjectOutputStream(bos);
            os.writeObject(obj);
            String bytesToHexString = this.bytesToHexString(bos.toByteArray());
            e.putString(key, bytesToHexString);
            e.commit();
        } catch (IOException var8) {
            var8.printStackTrace();
            Log.e("", "保存obj失败");
        }

    }

    public Object getObject(Context context, String key) {
        try {
            SharedPreferences e = context.getSharedPreferences(this.FILE_NAME, 0);
            if(e.contains(key)) {
                String string = e.getString(key, "");
                if(TextUtils.isEmpty(string)) {
                    return null;
                }

                byte[] stringToBytes = this.StringToBytes(string);
                ByteArrayInputStream bis = new ByteArrayInputStream(stringToBytes);
                ObjectInputStream is = new ObjectInputStream(bis);
                Object readObject = is.readObject();
                return readObject;
            }
        } catch (StreamCorruptedException var9) {
            var9.printStackTrace();
        } catch (IOException var10) {
            var10.printStackTrace();
        } catch (ClassNotFoundException var11) {
            var11.printStackTrace();
        }

        return null;
    }

    public Object get(Context context, String key, Object defaultObject) {
        SharedPreferences sp = context.getSharedPreferences(this.FILE_NAME, 0);
        return defaultObject instanceof String ?sp.getString(key, (String)defaultObject):(defaultObject instanceof Integer ? Integer.valueOf(sp.getInt(key, ((Integer)defaultObject).intValue())):(defaultObject instanceof Boolean ? Boolean.valueOf(sp.getBoolean(key, ((Boolean)defaultObject).booleanValue())):(defaultObject instanceof Float ? Float.valueOf(sp.getFloat(key, ((Float)defaultObject).floatValue())):(defaultObject instanceof Long ? Long.valueOf(sp.getLong(key, ((Long)defaultObject).longValue())):null))));
    }

    public void remove(Context context, String key) {
        SharedPreferences sp = context.getSharedPreferences(this.FILE_NAME, 0);
        Editor editor = sp.edit();
        editor.remove(key);
        SharedPreferencesCompat.apply(editor);
    }

    public void clear(Context context) {
        SharedPreferences sp = context.getSharedPreferences(this.FILE_NAME, 0);
        Editor editor = sp.edit();
        editor.clear();
        SharedPreferencesCompat.apply(editor);
    }

    public boolean contains(Context context, String key) {
        SharedPreferences sp = context.getSharedPreferences(this.FILE_NAME, 0);
        return sp.contains(key);
    }

    public Map<String, ?> getAll(Context context) {
        SharedPreferences sp = context.getSharedPreferences(this.FILE_NAME, 0);
        return sp.getAll();
    }

    private String bytesToHexString(byte[] bArray) {
        if(bArray == null) {
            return null;
        } else if(bArray.length == 0) {
            return "";
        } else {
            StringBuffer sb = new StringBuffer(bArray.length);

            for(int i = 0; i < bArray.length; ++i) {
                String sTemp = Integer.toHexString(255 & bArray[i]);
                if(sTemp.length() < 2) {
                    sb.append(0);
                }

                sb.append(sTemp.toUpperCase());
            }

            return sb.toString();
        }
    }

    private byte[] StringToBytes(String data) {
        String hexString = data.toUpperCase().trim();
        if(hexString.length() % 2 != 0) {
            return null;
        } else {
            byte[] retData = new byte[hexString.length() / 2];

            for(int i = 0; i < hexString.length(); ++i) {
                char hex_char1 = hexString.charAt(i);
                int int_ch1;
                if(hex_char1 >= 48 && hex_char1 <= 57) {
                    int_ch1 = (hex_char1 - 48) * 16;
                } else {
                    if(hex_char1 < 65 || hex_char1 > 70) {
                        return null;
                    }

                    int_ch1 = (hex_char1 - 55) * 16;
                }

                ++i;
                char hex_char2 = hexString.charAt(i);
                int int_ch2;
                if(hex_char2 >= 48 && hex_char2 <= 57) {
                    int_ch2 = hex_char2 - 48;
                } else {
                    if(hex_char2 < 65 || hex_char2 > 70) {
                        return null;
                    }

                    int_ch2 = hex_char2 - 55;
                }

                int int_ch = int_ch1 + int_ch2;
                retData[i / 2] = (byte)int_ch;
            }

            return retData;
        }
    }

    private static class SharedPreferencesCompat {
        private static final Method sApplyMethod = findApplyMethod();

        private SharedPreferencesCompat() {
        }

        private static Method findApplyMethod() {
            try {
                Class clz = Editor.class;
                return clz.getMethod("apply", new Class[0]);
            } catch (NoSuchMethodException var1) {
                return null;
            }
        }

        public static void apply(Editor editor) {
            try {
                if(sApplyMethod != null) {
                    sApplyMethod.invoke(editor, new Object[0]);
                    return;
                }
            } catch (IllegalArgumentException var2) {
                ;
            } catch (IllegalAccessException var3) {
                ;
            } catch (InvocationTargetException var4) {
                ;
            }

            editor.commit();
        }
    }
}
